//index.js
//获取应用实例
const app = getApp();
var common = require('../../../service/common.js');
var user = require('../../../service/user.js');
var gwang = require('../../../service/gwang.js');
var startPageX = 0;
var movePageX = 0;
var currentIndex = -1;
Page({
    data: {
        search_click: false,
        userInfo: {},
        items: [],
        page: 1,
        isShow: false,
        searchList: [],
        isNull: false,
        search:"",
        related:null,
        gw:null
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../logs/logs/logs'
        })
    },
    onPullDownRefresh: function () {
        var _this = this;
        _this.setData({
            page: 1
        })
        _this.loadWebsites();
    },
    onReachBottom: function() {
      console.log(this.data.isNull)
        if (this.data.isNull == false) {
            this.data.page+=1
            this.setData({
              page: this.data.page
            })
            this.loadWebsites();
        }
    },
    onLoad: function () {
      wx.showLoading({
        title: '加载中...',
      })
        var _this = this;
    },
    // 关联操作
    guanlian:function(e){
      if (this.data.related != null) {
          wx.showModal({
            title: '提示',
            content: '确定更改官网？',
            success:res=>{
              if (res.confirm){
                gwang.Guanl(this.data.userInfo.openid, e.currentTarget.dataset.id, res => {
                  if (res.data.code == 0) {
                    wx.showToast({
                      title: '关联成功',
                    })
                    setTimeout(()=>{
                      wx.navigateBack()
                    },1000)
                  }
                })
              }
            }
          })
      }else{
        gwang.Guanl(this.data.userInfo.openid, e.currentTarget.dataset.id, res => {
          if (res.data.code == 0) {
            wx.showToast({
              title: '关联成功',
            })
          }
          setTimeout(() => {
            wx.navigateBack()
          }, 500)
          console.log(res)
        })
      }
      
    },
    onShow: function () {
        var _this = this;
        _this.setData({
            userInfo: app.globalData.userInfo,
            isShow: false
        });  
        this.loadWebsites()
        // var token = wx.getStorageSync('token'), token_expire = wx.getStorageSync('token_expire'), nowTime = Date.now();
        
    },
    // 前往我的小官网
    my_gwang:function(){
      if (this.data.gw == null && this.data.related==null){//没有关联的官网也没有自己的官网
        wx.navigateTo({
          url: '/pages/manage/create/create',
        })        
      }else{
        wx.navigateTo({
          url: '/pages/manage/index/index',
        }) 
      }
    },
    // 获取官网
    loadWebsites: function () {
      user.getToken(this.data.userInfo.openid,token=>{
        common.fetchData(
          common.getUrl("website.websites"),
          { token: token, page: this.data.page, name: this.data.search },
          (res) => {
            console.log(res)
            if (this.data.page == 1) {
              wx.hideLoading()
              this.setData({
                items: res.data.data,
                related: res.data.related,
                gw: res.data.gw
              })
            } else {
              if (res.data.data.length != 0) {
                this.data.items = this.data.items.concat(res.data.data)
                this.setData({
                  items: this.data.items
                })
              } else {
                this.setData({
                  isNull: true
                })
              }
            }
          },
          "GET"
        )
      })
      
    },
    // 模糊搜索公司名称
    searchCompany: function (e) {
      this.setData({
        search: e.detail.value,
        page:1
      })
      this.loadWebsites()
    },
    // 隐藏搜索列表
    hideSearchBox: function () {
        var _this = this;
        _this.setData({
            searchList: []
        })
    },
    // 点击搜索框时显示取消按钮及首页原内容
    searchOn: function(e){
      this.setData({
        search_click: true
      })
    },
    // 点击取消时隐藏取消按钮及搜索内容
    searchOff: function(){
      this.setData({
        search_click: false,
        page:1,
        search:""
      })
      this.loadWebsites()
    },
    /**搜索框失去焦点 */
    searchBlur: function(e){
      this.setData({
        search_text: ''
      })
    },
    touchStart: function (e) {
      // this.setData({
      //   touchStart: e.changedTouches[0].pageX
      // })
      // currentIndex = e.currentTarget.dataset.index;
      // startPageX = e.changedTouches[0].pageX;
    },
    touchMove: function (e) {
      this.setData({
        touchMove: true
      })
      movePageX = e.changedTouches[0].pageX;
      // console.log('movePageX:' + movePageX, 'startPageX:' + startPageX);
      if( startPageX - movePageX > 60){
          this.showHandle();
      }
      if( startPageX - movePageX < -60 ){
          this.hideHandle();
      }
      if( startPageX <= movePageX + 60 && this.data.currentIndex!=-1 ){
          console.log('我执行了第174行')
          this.hideHandle();
          this.setData({
              currentIndex: -1
          })
      }
    },
    touchEnd: function (e) {
      
    },
    showHandle: function(){
        console.log('showHandle');
        this.setData({
          currentIndex: currentIndex
        })
    },
    hideHandle: function(){
        this.setData({
          currentIndex: -1
        })
    },

    // 标星置顶
    stickFuc: function (e) {
        var _this = this,
            clickStick = e.currentTarget.dataset.id,
            reqJson = {
                website_id : clickStick,
                token : utils.getToken(app)
            },
            sucFuc = function (res) {
                if (res.data.code == 0) {
                    _this.setData({
                        page: 1,
                        items: [],
                        currentIndex: -1
                    })
                    _this.loadWebsites(1);
                }
            },
            errFuc = function (res) {
                utils.tipBox({ content: res.data.message, cancel: false });
            };
        common.getAjaxData(common.getUrl('website.starWebsite'), reqJson, sucFuc, errFuc);
    },

    // 删除功能
    delFuc: function (e) {
        var _this = this,
            clickStick = e.currentTarget.dataset.id,
            clickFuc = function (res) {
                var reqJson = {
                    website_id: clickStick,
                    token: utils.getToken(app)
                },
                sucFuc = function (res) {
                    if (res.data.code == 0) {
                        _this.setData({
                            page: 1,
                            items: [],
                            currentIndex: -1
                        })
                        _this.loadWebsites(1);
                    }
                },
                errFuc = function (res) {
                    utils.tipBox({ content: res.data.message, cancel: false });
                };
                common.getAjaxData(common.getUrl('website.delAccessWebsite'), reqJson, sucFuc, errFuc);
            };
        utils.tipBox({ content: '您确定要删除吗？', sucFuc: clickFuc });
    },


    /**跳转详情页 */
    networkDetail: function(e){
        var data_id = e.currentTarget.dataset.id
        wx.navigateTo({
          url: '/pages/handle/details/details?id=' + data_id,
        })
    }
})
